package com.lyp.adapters;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.MaterialDialog;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lyp.jsonbean.DetailsSingeBean;
import com.lyp.jsonbean.JlSingeDetectionBean;
import com.lyp.jsonbean.LoginBean;
import com.lyp.jsonbean.RecheckBean;
import com.tjl.yangxixis.R;
import com.tjl.yangxixis.api.YangxixiApi;

import java.io.IOException;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 作者:ChenXi 时间:2017/5/31 14: 26
 */
//抢单(检测、初检)/接单(复检)
public class SingeAdapter extends RecyclerView.Adapter<SingeAdapter.MyViewHolder>{

    private List<JlSingeDetectionBean.DataBean> mList;
    private SingeAdapter.MyItemClickListener mListener;
    private Context mContext;
    private LoginBean.DataBean mLoginbean;
    public SingeAdapter(List<JlSingeDetectionBean.DataBean> list,Context context,LoginBean.DataBean loginbean) {
        mList = list;
        mContext = context;
        mLoginbean = loginbean;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        SingeAdapter.MyViewHolder holder = new SingeAdapter.MyViewHolder(
                LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_singe_item, viewGroup,
                        false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder,final int i) {
        myViewHolder.mTime.setText(mList.get(i).getExpected_time());//期望时间
        myViewHolder.mType.setText(mList.get(i).getDetection_type());//检测类型
        myViewHolder.mRegion.setText(mList.get(i).getU_addres());//地区
        myViewHolder.mQuote.setText(mList.get(i).getDetection_price());//报价
        myViewHolder.mHome.setVisibility(View.VISIBLE);
        if (mList.get(i).getDetection_type() != null) {
            if (mList.get(i).getDetection_type().equals("复检")) {
                myViewHolder.mBtnjoin.setVisibility(View.VISIBLE);
                myViewHolder.mBtnjoin.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mList.get(i).getCash_deposit() == 0) {
                            if (mList.get(i).getReminder_type() >= 1) {
                                new MaterialDialog.Builder(mContext)
                                        .title("无法接单！")
                                        .content("快联系致蓝网络科技！")
                                        .positiveText("确定")
                                        .canceledOnTouchOutside(false)
                                        .cancelable(false)
                                        .show();
                                return;
                            }
                        }
                        try {
                            Recheck(mList.get(i).getO_id(), mLoginbean.getC_id(), i);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } else if (mList.get(i).getDetection_type().equals("初检")) {
                myViewHolder.mBtngrab.setVisibility(View.VISIBLE);
                myViewHolder.mBtngrab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mList.get(i).getCash_deposit() == 0) {
                            if (mList.get(i).getReminder_type() >= 1) {
                                new MaterialDialog.Builder(mContext)
                                        .title("无法抢单")
                                        .content("快联系致蓝网络科技！")
                                        .positiveText("确定")
                                        .canceledOnTouchOutside(false)
                                        .cancelable(false)
                                        .show();
                            }
                            return;
                        }
                        try {
                            detailsSinge(mList.get(i).getO_id(), mLoginbean.getC_id(), i);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } else if (mList.get(i).getDetection_type().equals("检测")) {
                myViewHolder.mBtngrab.setVisibility(View.VISIBLE);
                myViewHolder.mBtngrab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mList.get(i).getCash_deposit() == 0) {
                            if (mList.get(i).getReminder_type() >= 1) {
                                new MaterialDialog.Builder(mContext)
                                        .title("无法抢单")
                                        .content("快联系致蓝网络科技！")
                                        .positiveText("确定")
                                        .canceledOnTouchOutside(false)
                                        .cancelable(false)
                                        .show();
                            }
                            return;
                        }
                        try {
                            detailsSinge(mList.get(i).getO_id(), mLoginbean.getC_id(), i);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            } else if (mList.get(i).getDetection_type().equals("")) {
                myViewHolder.mBtngrab.setVisibility(View.VISIBLE);
                myViewHolder.mBtngrab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mList.get(i).getCash_deposit() == 0) {
                            if (mList.get(i).getReminder_type() >= 1) {
                                new MaterialDialog.Builder(mContext)
                                        .title("无法抢单")
                                        .content("快联系致蓝网络科技！")
                                        .positiveText("确定")
                                        .canceledOnTouchOutside(false)
                                        .cancelable(false)
                                        .show();
                            }
                            return;
                        }
                        try {
                            detailsSinge(mList.get(i).getO_id(), mLoginbean.getC_id(), i);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }
    @Override
    public int getItemCount() {
        return mList.size();
    }

    /**
     * 设置Item点击监听
     * @param listener
     */
    public void setOnItemClickListener(SingeAdapter.MyItemClickListener listener){
        this.mListener = listener;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        TextView mType;
        TextView mTime;
        TextView mRegion;//地区
        TextView mQuote;//报价
        TextView mHome;//是否居家
        RelativeLayout mSingeAll;
        Button mBtnjoin,mBtngrab;

        public MyViewHolder(View view)
        {
            super(view);
            mType = (TextView) view.findViewById(R.id.tv_server_type);
            mTime = (TextView) view.findViewById(R.id.tv_qwtime);
            mRegion = (TextView) view.findViewById(R.id.tv_region);
            mQuote = (TextView) view.findViewById(R.id.tv_quote);
            mHome = (TextView) view.findViewById(R.id.tv_jujia);
            mBtngrab = (Button) view.findViewById(R.id.btn_grab_single);//抢单
            mBtnjoin = (Button) view.findViewById(R.id.btn_join_single);//接单
            mSingeAll = (RelativeLayout) view.findViewById(R.id.rl_singeAll);
            mSingeAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(v,getPosition());
                }
            });
        }
    }
    /**
     * item点击接口
     */
    public interface MyItemClickListener {
        void onItemClick(View v, int position);
    }

    //复检接单
    public void Recheck(String o_id,String c_id, final int i) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<RecheckBean> call = api.getRecheck(o_id,c_id);
        call.enqueue(new Callback<RecheckBean>() {
            @Override
            public void onResponse(Call<RecheckBean> call, final Response<RecheckBean> response) {
                if (response.body().getResult() == 1 ) {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(mContext)
                                    .title("接单成功")
                                    .content("快去未分配任务进行分配!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .show();
                            mList.remove(i);
                            SingeAdapter.this.notifyDataSetChanged();
                            Toast.makeText(mContext,response.body().getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                else {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            ToastUtils.showShort(response.body().getMessage());
                        }
                    });
                }
            }
            @Override
            public void onFailure(Call<RecheckBean> call, Throwable t) {
               ToastUtils.showShort(t.getMessage());
            }
        });
    }

    //初检、检测抢单
    public void detailsSinge(String o_id,String c_id, final int i) throws IOException {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(YangxixiApi.APP_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        YangxixiApi api = retrofit.create(YangxixiApi.class);
        Call<DetailsSingeBean> call = api.getDetailsSinge(o_id,c_id);
        call.enqueue(new Callback<DetailsSingeBean>() {
            @Override
            public void onResponse(Call<DetailsSingeBean> call, final Response<DetailsSingeBean> response) {
                if (response.body().getResult() == 1 ) {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            new MaterialDialog.Builder(mContext)
                                    .title("抢单成功")
                                    .content("快去未分配任务进行分配!")
                                    .positiveText("确定")
                                    .canceledOnTouchOutside(false)
                                    .cancelable(false)
                                    .show();
                            mList.remove(i);
                            SingeAdapter.this.notifyDataSetChanged();
                            Toast.makeText(mContext,response.body().getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                else {
                    new Handler().post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mContext,response.body().getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
            @Override
            public void onFailure(Call<DetailsSingeBean> call, Throwable t) {
                LogUtils.i("tag","失败"+t.getMessage());
            }
        });
    }
}
